import 'dart:ffi';

import 'package:flutter/material.dart';
import 'package:full_display/common/qxp_color.dart';
import 'package:full_display/common/qxp_style.dart';
import 'package:full_display/generated/l10n.dart';
import 'package:full_display/provide/cmd_provider.dart';
import 'package:full_display/provide/drawer_provider.dart';
import 'package:full_display/unit/asset_utils/asset_utils.dart';
import 'package:full_display/unit/screen/screen_util1.dart';
import 'package:full_display/unit/screen/screen_utility.dart';
import 'package:full_display/unit/web_socket/web_sockect_utils.dart';
import 'package:full_display/unit/widgets/button/button_addSub_widget.dart';
import 'package:full_display/unit/widgets/button/button_text_widget.dart';
import 'package:full_display/unit/widgets/button/button_widget.dart';
import 'package:full_display/unit/widgets/slider/slider_widget.dart';
import 'package:provider/provider.dart';

/// 抽屉View
class DrawerWidget extends StatelessWidget {
  DrawerWidget({Key? key}) : super(key: key);

  late DrawerProvider _drawerData;
  late CMDProvider _cmdData;

  @override
  void dispose() {}

  @override
  Widget build(BuildContext context) {
    final double topPadding = MediaQuery.of(context).padding.top;
    _drawerData = context.watch<DrawerProvider>();
    _cmdData = context.watch<CMDProvider>();
    print("deviceSettingInfo++++++++++++${_cmdData.deviceSettingInfo}");
    return Drawer(
      backgroundColor: ColorPlate.background,
      child: SingleChildScrollView(
        child: Column(
          children: [
            Container(
              padding: EdgeInsets.only(
                  top: ScreenUtil1.statusBarHeight + 10.0.H,
                  left: 20.0.W,
                  right: 20.0.W),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text(
                        S.of(context).playandpause,
                        style: StandardTextStyle.textWhite12Style,
                      ),
                      InkWell(
                        onTap: () {
                          if (_cmdData.playpause == false) {
                            _cmdData.setPlayPauseState(true);
                            WebSocketUtils().sendMessage(5);
                          } else {
                            _cmdData.setPlayPauseState(false);
                            WebSocketUtils().sendMessage(3);
                          }
                        },
                        child: Image.asset(
                          height: 24.0.H,
                          width: 24.0.W,
                          _cmdData.playpause == false
                              ? AssetUtils.getAsset2Image("icon_play")
                              : AssetUtils.getAsset2Image("icon_pause"),
                        ),
                      ),
                    ],
                  ),
                  Container(
                    padding: EdgeInsets.only(top: 20.0.H),
                    child: Text(
                      S.of(context).playModel,
                      style: StandardTextStyle.textWhite12Style,
                    ),
                  ),
                  SizedBox(
                    height: 5.0.H,
                  ),
                  GridView.builder(
                    padding: EdgeInsets.zero,
                    scrollDirection: Axis.vertical,
                    physics: const NeverScrollableScrollPhysics(),
                    shrinkWrap: true,
                    itemCount: _drawerData.playModelList.length,
                    itemBuilder: (con, index) {
                      return ButtonTextWidget(
                        title: _drawerData.playModelList[index]['title'],
                        state: _drawerData.playModelList[index]['isSelect'],
                        width: 60.0.W,
                        height: 24.0.W,
                        index: index,
                        tapAction: playClick,
                      );
                    },
                    gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                      crossAxisCount: 3,
                      crossAxisSpacing: 12.0.W,
                      //宽高比
                      childAspectRatio: 2 / 1,
                    ),
                  ),
                  // Container(
                  //   padding: EdgeInsets.only(top: 20.0.H),
                  //   child: Text(
                  //     S.of(context).sewentijie,
                  //     style: StandardTextStyle.textWhite12Style,
                  //   ),
                  // ),
                  // SizedBox(
                  //   height: 10.0.H,
                  // ),
                  // GridView.builder(
                  //   padding: EdgeInsets.zero,
                  //   scrollDirection: Axis.vertical,
                  //   physics: const NeverScrollableScrollPhysics(),
                  //   shrinkWrap: true,
                  //   itemCount: _drawerData.colorList.length,
                  //   itemBuilder: (con, index) {
                  //     return ButtonTextWidget(
                  //       title: _drawerData.colorList[index]['title'],
                  //       state: _drawerData.colorList[index]['isSelect'],
                  //       width: 60.0.W,
                  //       height: 24.0.W,
                  //       index: index,
                  //       tapAction: colorClick,
                  //     );
                  //   },
                  //   gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                  //     crossAxisCount: 3,
                  //     crossAxisSpacing: 12.0.W,
                  //     //宽高比
                  //     childAspectRatio: 2 / 1,
                  //   ),
                  // ),
                  Container(
                    padding: EdgeInsets.only(top: 20.0.H),
                    child: Text(
                      S.of(context).dianjiqidongmodel,
                      style: StandardTextStyle.textWhite12Style,
                    ),
                  ),
                  GridView.builder(
                    padding: EdgeInsets.zero,
                    scrollDirection: Axis.vertical,
                    physics: const NeverScrollableScrollPhysics(),
                    shrinkWrap: true,
                    itemCount: _drawerData.runModelList.length,
                    itemBuilder: (con, index) {
                      return ButtonTextWidget(
                        title: _drawerData.runModelList[index]['title'],
                        state: _drawerData.runModelList[index]['isSelect'],
                        width: 120.0.W,
                        height: 24.0.W,
                        index: index,
                        tapAction: dianjiClick,
                      );
                    },
                    gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                      crossAxisCount: 2,
                      crossAxisSpacing: 24.0.W,
                      //宽高比
                      childAspectRatio: 15 / 5,
                    ),
                  ),
                ],
              ),
            ),
            SliderWidget(
              title: S.of(context).liangdutiaojie,
              state: 'light',
              minValue: 0.0,
              maxValue: 15.0,
              sliderValue:
                  _cmdData.deviceSettingInfo['properties']['brightness'] ?? 0,
            ),
            SliderWidget(
              title: S.of(context).volume_contrl,
              state: 'voice',
              minValue: 0.0,
              maxValue: 15.0,
              sliderValue:
                  _cmdData.deviceSettingInfo['properties']['volume'] ?? 0,
            ),
            SliderWidget(
              title: S.of(context).speed_regulation,
              state: 'run',
              minValue: 60.0,
              maxValue: 90.0,
              sliderValue: 60,
            ),
            ButtonAddSubWidget(title: S.of(context).jiaodutiaozheng),
            GridView.builder(
              padding: EdgeInsets.zero,
              scrollDirection: Axis.vertical,
              physics: const NeverScrollableScrollPhysics(),
              shrinkWrap: true,
              itemCount: _drawerData.deviceFunList.length,
              itemBuilder: (con, index) {
                return ButtonWidget(
                  title: _drawerData.deviceFunList[index]['title'],
                  imgName: _drawerData.deviceFunList[index]['img'],
                  index: index,
                  iconWidth: 32.0.W,
                  iconHeight: 32.0.W,
                  tapAction: stateClick,
                );
              },
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 3,
                crossAxisSpacing: 20.0.W,
              ),
            ),
          ],
        ),
      ),
    );
  }

//电机点击
  void dianjiClick(int index) {
    _drawerData.changeRunState(index);
    if (index == 0) {
      WebSocketUtils().sendMessage(37);
    } else if (index == 1) {
      WebSocketUtils().sendMessage(38);
    } else if (index == 2) {
      WebSocketUtils().sendMessage(39);
    }
  }

//色温点击
  void colorClick(int index) {
    _drawerData.changeColorState(index);
    switch (index) {
      case 0:
        WebSocketUtils().sendCustomMessage(52,
            content: {"value": 24, 'r': 32768, 'g': 50000, 'b': 50000});
        break;
      case 1:
        WebSocketUtils().sendCustomMessage(52,
            content: {"value": 24, 'r': 52768, 'g': 32768, 'b': 32768});
        break;
      case 2:
        // WebSocketUtils().sendCustomMessage(52,
        //     content: {"value": 24, 'r': 65536, 'g': 65536, 'b': 65536});
        WebSocketUtils().sendCustomMessage(52,
            content: {"value": 24, 'r': 32768, 'g': 32768, 'b': 32768});
        break;
      case 3:
        WebSocketUtils().sendCustomMessage(52,
            content: {"value": 24, 'r': 32768, 'g': 32768, 'b': 32768});
        break;
      default:
    }
  }

//播放模式点击
  void playClick(int index) {
    _drawerData.changePlayState(index);
    switch (index) {
      case 0:
        WebSocketUtils().sendMessage(33, content: 1);
        break;
      case 1:
        WebSocketUtils().sendMessage(33, content: 3);
        break;
      case 2:
        WebSocketUtils().sendMessage(33, content: 2);
        break;
      default:
    }
  }

  //按钮点击
  void stateClick(int index) {
    switch (index) {
      case 0:
        WebSocketUtils().sendCustomMessage(52, content: {"value": 5});
        break;
      case 1:
        WebSocketUtils().sendMessage(2);
        break;
      case 2:
        WebSocketUtils().sendCustomMessage(52, content: {"value": 21});
        break;
      case 3:
        WebSocketUtils().sendCustomMessage(52, content: {"value": 22});
        break;
      case 4:
        WebSocketUtils().sendCustomMessage(52, content: {"value": 14});
        break;

      default:
    }
  }
}
